package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.procurement.ProductQuality;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class ProductQualityServiceImpl extends AbstractBaseService {
	
	public static final String COLUMN_POI_ID = "poiId";
	
	public static final String COLUMN_PI_REFERENCE = "reference";
	
	public static final String COLUMN_POI_COLORS = "colors";
	
	public static final String COLUMN_POI_ORDER_NUMBER = "orderNumber";
	
	private ProductIdentificationServiceImpl piServiceImpt;
	
	private CtServiceImpl ctService;
	
	public ProductQualityServiceImpl(){
		super();
		this.piServiceImpt = new ProductIdentificationServiceImpl();
		this.ctService = new CtServiceImpl();
	}
	@Override
	public String delete(int id) throws Exception {
		return super.delete(ProductQuality.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductQuality obj = (ProductQuality)this.dao.get(ProductQuality.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		
		xmlSB.append("<" + COLUMN_POI_ID + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(obj.getProductOrderItem().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_POI_ID + ">");
		
		xmlSB.append("<" + COLUMN_PI_REFERENCE + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(obj.getProductOrderItem().getProductIdentification().getBiReference().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_PI_REFERENCE + ">");
		
		xmlSB.append("<" + COLUMN_POI_COLORS + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(obj.getProductOrderItem().getColorNumber());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_POI_COLORS + ">");
		
		xmlSB.append("<" + COLUMN_POI_ORDER_NUMBER + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(obj.getProductOrderItem().getProductIdentification().getOrderNumber());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_POI_ORDER_NUMBER + ">");
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		this.piServiceImpt.setLocale(this.locale);
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<ProductQuality> objList = this.dao.getAllPagination(ProductQuality.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductQuality obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			
			xmlSB.append("<" + COLUMN_POI_ID + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getProductOrderItem().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_POI_ID + ">");
			
			xmlSB.append("<" + COLUMN_POI_COLORS + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getProductOrderItem().getColorNumber());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_POI_COLORS + ">");
			
			xmlSB.append(this.piServiceImpt.toXMLFragment(obj.getProductOrderItem().getProductIdentification()));
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductQuality.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public ProductQuality getObj(Integer id) throws Exception{
		return (ProductQuality)this.dao.get(ProductQuality.class.getName(), id);
	}
	
	public String getAll(Integer piId) throws Exception{
		return null;
	}	

	public static void main(String[] args) throws Exception{
		ProductQualityServiceImpl os = new ProductQualityServiceImpl();
		System.out.println(os.getAllPagination(1, "id", "asc", null));
		//char index = 'C';
		//System.out.println(++index);
	}
}



